package com.niiwoo.civet.trade.listener;

import com.niiwoo.civet.trade.constant.MqConstant;
import com.niiwoo.civet.trade.dto.request.loan.AllotChannelConfigModifyDTO;
import com.niiwoo.civet.trade.service.local.loan.SpeedLoanDispenseChannelService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 分端配置刷新监听器
 * @author TomXue
 * @since 2018-09-11
 */
@Component
@Slf4j
public class AllotChannelConfigModifyListener {
    @Autowired
    private SpeedLoanDispenseChannelService speedLoanDispenseChannelService;

    @RabbitListener(queues = {MqConstant.ALLOT_CHANNEL_CONFIG_MODIFY_QUEUE})
    public void allotChannelConfigModifyHandler(AllotChannelConfigModifyDTO allotChannelConfigModifyDTO, Message message, Channel channel) throws IOException {
        try {
            log.info("分端配置刷新通知,渠道ID={},开始刷新", allotChannelConfigModifyDTO == null ? null : allotChannelConfigModifyDTO.getChannelId());
            speedLoanDispenseChannelService.refreshChannel();
            log.info("分端配置刷新通知,渠道ID={},结束刷新", allotChannelConfigModifyDTO == null ? null : allotChannelConfigModifyDTO.getChannelId());
        } catch (Exception e) {
            log.error("分端配置刷新通知,渠道ID={},发生系统异常,原因={}", allotChannelConfigModifyDTO == null ? null : allotChannelConfigModifyDTO.getChannelId(), e);
        } finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}
